﻿$(document).ready(function() {
    $("#btnPic").click(function() {
        
    });

    $("#btnPass").click(function() {
        showEditPassword();
    });

    $('#ModalDialog').on('click', '#btnSavePass', function() {
        var sendData = {
            "OldPassword": $("#oldPass").val(),
            "NewPassword": $("#newPass1").val(),
            "NewPasswordExt": $("#newPass2").val()
        };

        $.ajax({
            type: "POST",
            url: 'MyAccount.aspx/ChangePassword',
            data: JSON.stringify(sendData),
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function(data) {
                var result = data.d;
                if (result[0] == 1) {
                    $("#ModalDialog").closeCZDialog();
                    alert(result[1]);
                } else {
                    $("#divMsg").html(result[1]);
                }
            }
        });
        return false;
    });

    $("#ModalDialog").on("keypress", "#oldPass, #newPass1, #newPass2", function(e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $("#btnSavePass").click();
        }
    });

    $("#btnCEmail").click(function() {
        $("#spnEmail, #btnPass, #btnEditApp, #btnCEmail").hide();
        $("#txtEmail, #btnCancelEmail, #btnSaveEmail").fadeIn(300);
    });

    $("#btnCancelEmail, #btnSaveEmail").click(function() {
        $("#txtEmail, #btnCancelEmail, #btnSaveEmail").hide();
        $("#spnEmail, #btnPass, #btnEditApp, #btnCEmail").fadeIn(300);
    });

    $("#txtEmail").keypress(function(e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $("#btnSaveEmail").click();
        }
    });

    $("#btnSaveEmail").click(function() {
        var sendData = {
            "EmailAddress": $("#txtEmail").val()
        };

        $.ajax({
            type: "POST",
            url: 'MyAccount.aspx/ChangeEmail',
            data: JSON.stringify(sendData),
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function(data) {
                var result = data.d;
                if (result[0] == 1) {
                    $("#spnEmail").text($("#txtEmail").val());
                }
                alert(result[1]);
                $("#txtEmail, #btnCancelEmail, #btnSaveEmail").hide();
                $("#spnEmail, #btnPass, #btnEditApp, #btnCEmail").fadeIn(300);
            }
        });
        return false;
    });

    $("#btnEditApp").click(function() {
        $("#spnApp, #btnEditApp, #btnCEmail, #btnEditApp, #btnPass").hide();
        $("#cbAppOpt, #btnCancelApp, #btnSaveApp").fadeIn(300);
    });

    $("#btnSaveApp").click(function() {
        var sendData = {
            "Availability": $("#cbAppOpt").val()
        };

        $.ajax({
            type: "POST",
            url: 'MyAccount.aspx/ChangeAvailability',
            data: JSON.stringify(sendData),
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function(data) {
                var result = data.d;
                alert(result[1]);
                window.location.href = "MyAccount.aspx";
            }
        });
        return false;
    });

    $("#btnCancelApp").click(function() {
        $("#cbAppOpt, #btnCancelApp, #btnSaveApp").hide();
        $("#spnApp, #btnEditApp, #btnCEmail, #btnEditApp, #btnPass").fadeIn(300);
    });

    $("#btnSaveApp").click(function() {
        $("#cbAppOpt, #btnCancelApp, #btnSaveApp").hide();

        //$("#spnApp").text($("#cbAppOpt").text());
        $("#spnApp, #btnEditApp, #btnCEmail, #btnEditApp, #btnPass").fadeIn(300);
    });

});

function showEditPassword() {
    var txtTitle = 'Change Password:';
    var dTitle = '<div style="border-bottom:1px dotted #000;padding:5px 0;font-size:14px;' +
        'font-weight:bold;margin:10px;text-align:center;font-family:Arial;">' +
        txtTitle + '</div>';
    
    var divBox = '<div style="margin-top:30px;">';
    var div1 = '<div style="position:relativ;margin-bottom:6px;"><div class="fl_left" style="width:150px;text-align:right;font-size:12px;">';
    var div2 = '<div class="fl_left" style="width:210px;margin-left:5px;">';

    var oldPass = '<input type="password" name="oldPass" id="oldPass" value="" ';
    oldPass += 'style="font-size:12px;width:210px;" />';
    var newPass1 = '<input type="password" name="newPass[]" id="newPass1" value="" ';
    newPass1 += 'style="font-size:12px;width:210px;" />';
    var newPass2 = '<input type="password" name="newPass[]" id="newPass2" value="" ';
    newPass2 += 'style="font-size:12px;width:210px;" />';

    var txt1 = "Current Password : ";
    var txt2 = "New Password : ";
    var txt3 = "Confirm New Password : ";
        
    var content = divBox;
    content += div1 + txt1 + '</div>';
    content += div2 + oldPass + '</div><div class="cl_left"></div></div>';
    content += div1 + txt2 + '</div>';
    content += div2 + newPass1 + '</div><div class="cl_left"></div></div>';
    content += div1 + txt3 + '</div>';
    content += div2 + newPass2 + '</div><div class="cl_left"></div></div>';
    content += '</div>';
    content += '<div id="divMsg" style="margin-top:12px;color:Red;text-align:center;font-size:14px;"></div>';

    var dButton = '<div id="divSavePass" style="position:absolute;bottom:27px;left:0;width:100%;text-align:center;">' +
        '<div id="btnCancel" class="btnSmall" title="Cancel" style="padding:3px 21px;margin-right:10px;">Cancel</div>' +
        '<div id="btnSavePass" class="btnSmall" title="Change Password" style="padding:3px 21px;">Update</div>' +
        '</div>';

    var boxContent = dTitle + content + dButton;
    $('#ModalDialog').html(boxContent).showCZDialog({
        width: 420, height: 290, closeButton: '#btnCancel'
    });
    $('#divModalWrap').css("background", "#FFF");
}